<template>
  <div class="main-content">
    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="课程名称">
        <el-input v-model="searchForm.kechengmingcheng" placeholder="课程名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="课程类型">
        <el-input v-model="searchForm.kechengleixing" placeholder="课程类型" clearable></el-input>
      </el-form-item>
      <el-form-item label="学期">
        <el-input v-model="searchForm.xueqi" placeholder="学期" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="kechengmingcheng" label="课程名称" width="180"></el-table-column>
      <el-table-column prop="kechengleixing" label="课程类型" width="180"></el-table-column>
      <el-table-column prop="xueqi" label="学期"></el-table-column>
      <el-table-column prop="banji" label="班级"></el-table-column>
      <el-table-column prop="jiaoshixingming" label="教师姓名"></el-table-column>
      <el-table-column prop="status" label="选课状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isSelected ? 'success' : 'info'">
            {{ scope.row.isSelected ? '已选' : '未选' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button 
            type="text" 
            size="small" 
            @click="handleSelect(scope.row)"
            v-if="!scope.row.isSelected">
            选课
          </el-button>
          <el-button 
            type="text" 
            size="small"
            style="color: #F56C6C"
            @click="handleCancel(scope.row)"
            v-if="scope.row.isSelected">
            退课
          </el-button>
          <el-button type="text" size="small" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 课程详情对话框 -->
    <el-dialog
      title="课程详情"
      :visible.sync="detailDialogVisible"
      width="50%"
      :before-close="handleDetailDialogClose">
      <div v-if="detailData" class="detail-content">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="课程名称">{{ detailData.kechengmingcheng }}</el-descriptions-item>
          <el-descriptions-item label="课程类型">{{ detailData.kechengleixing }}</el-descriptions-item>
          <el-descriptions-item label="学期">{{ detailData.xueqi }}</el-descriptions-item>
          <el-descriptions-item label="班级">{{ detailData.banji }}</el-descriptions-item>
          <el-descriptions-item label="上课时间">{{ detailData.xingqi }} {{ detailData.shangkeshijian }}</el-descriptions-item>
          <el-descriptions-item label="教师工号">{{ detailData.jiaoshigonghao }}</el-descriptions-item>
          <el-descriptions-item label="教师姓名">{{ detailData.jiaoshixingming }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ detailData.addtime }}</el-descriptions-item>
          <el-descriptions-item label="选课状态">
            <el-tag :type="detailData.isSelected ? 'success' : 'info'">
              {{ detailData.isSelected ? '已选' : '未选' }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDetailDialogClose">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {
        kechengmingcheng: '',
        kechengleixing: '',
        xueqi: ''
      },
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      detailDialogVisible: false,
      detailData: null
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    async getDataList() {
      try {
        const { data } = await this.$http({
          url: `xueshengxuanke2/list`,
          method: 'get',
          params: {
            page: this.page,
            limit: this.limit,
            kechengmingcheng: this.searchForm.kechengmingcheng,
            kechengleixing: this.searchForm.kechengleixing,
            xueqi: this.searchForm.xueqi
          }
        });

        if (data && data.code === 0) {
          this.tableData = data.data.list;
          this.total = data.data.total;
          
          // 并行检查所有课程的选课状态
          const statusChecks = this.tableData.map(course => 
            this.$http({
              url: `xueshengxuanke2/kecheng/${course.kechengmingcheng}`,
              method: 'get'
            }).then(res => {
              if (res.data && res.data.code === 0) {
                this.$set(course, 'isSelected', res.data.data);
              }
            }).catch(err => {
              console.error('检查课程状态失败:', err);
            })
          );
          
          // 等待所有状态检查完成
          await Promise.all(statusChecks);
        }
      } catch (error) {
        console.error('获取数据失败:', error);
        this.$message.error('获取数据失败');
      }
    },
    // 搜索
    handleSearch() {
      this.page = 1
      this.getDataList()
    },
    // 重置
    resetSearch() {
      this.searchForm = {
        kechengmingcheng: '',
        kechengleixing: '',
        xueqi: ''
      }
      this.page = 1
      this.getDataList()
    },
    // 每页数
    handleSizeChange(val) {
      this.limit = val
      this.page = 1
      this.getDataList()
    },
    // 当前页
    handleCurrentChange(val) {
      this.page = val
      this.getDataList()
    },
    // 选课
    handleSelect(row) {
      this.$confirm('确认选择该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: 'xueshengxuanke2/select',
          method: 'post',
          data: {
            kechengmingcheng: row.kechengmingcheng,
            kechengleixing: row.kechengleixing,
            xueqi: row.xueqi,
            banji: row.banji,
            jiaoshigonghao: row.jiaoshigonghao,
            jiaoshixingming: row.jiaoshixingming
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '选课成功',
              type: 'success',
              duration: 1500
            });
            // 更新课程状态
            row.isSelected = true;
          } else {
            this.$message.error(data.msg);
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消选课'
        });
      });
    },
    // 查看详情
    handleDetail(row) {
      this.$http({
        url: `xueshengxuanke2/detail`,
        method: 'get',
        params: {
          id: row.id
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.detailData = {
            ...data.data,
            isSelected: row.isSelected
          };
          this.detailDialogVisible = true;
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 关闭详情对话框
    handleDetailDialogClose() {
      this.detailDialogVisible = false;
      this.detailData = null;
    },
    // 退课
    handleCancel(row) {
      this.$confirm('确认退选该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: 'xueshengxuanke2/cancel',
          method: 'post',
          data: {
            kechengmingcheng: row.kechengmingcheng
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '退课成功',
              type: 'success',
              duration: 1500
            });
            // 更新课程状态
            row.isSelected = false;
          } else {
            this.$message.error(data.msg);
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退课'
        });
      });
    }
  }
}
</script>

<style scoped>
.main-content {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.detail-content {
  padding: 20px;
}

.detail-content .el-descriptions {
  margin-bottom: 20px;
}
</style> 